package com.project.controller;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.result.ExcelImportResult;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.bean.ClientBean;
import com.project.service.IClientService;
import com.project.util.ExcelUtil;
import com.project.util.InfoResult;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author lyl
 * description:
 */
@RestController
@RequestMapping("client")
@Api(tags = "客户controller")
public class ClientController {
    @Autowired
    private IClientService iClientService;

    @GetMapping("showAllClient")
    private List<ClientBean> showAll() {
        return iClientService.showAll();
    }

    //导出接口
    @GetMapping("exportExcel")
    public void export(HttpServletResponse response) {
        System.out.println("开始导出");
        // 模拟从数据库获取需要导出的数据
        List<ClientBean> personList = iClientService.showAll();
//        //设置序号（将id字段作为序号，导出后实现序号递增）
//        Integer i = 1;
//        for (ClientBean users : personList) {
//            users.setId(i++);
//        }
        // 导出操作
        ExcelUtil.exportExcel(personList, "sheet导出", "mySheet", ClientBean.class, ClientBean.class.getSimpleName() + ".xls", response);
    }

    @RequestMapping("importExcel")
    public String importExcel2(@RequestParam("file") MultipartFile file) {
        ImportParams importParams = new ImportParams();
        //表格标题行数,默认0
        importParams.setHeadRows(1);
        //表头行数,默认1
        importParams.setTitleRows(1);
        //是否需要校验上传的Excel,默认false
        importParams.setNeedVerify(false);
        try {
            ExcelImportResult<ClientBean> result = ExcelImportUtil.importExcelMore(file.getInputStream(), ClientBean.class, importParams);
            List<ClientBean> list = result.getList();
            for (ClientBean l : list) {
                System.out.println(l);
                System.out.println(iClientService.addClient(l));
            }
            System.out.println("从Excel导入数据一共 {} 行 " + list.size());
        } catch (IOException e) {
            System.out.println(e.getMessage());
        } catch (Exception e1) {
            System.out.println("导入失败：{}" + e1.getMessage());
        }
        return "导入成功";
    }


    @GetMapping("showClientByPage/{pageNum}/{clientName}/{clientScope}")
    Page<ClientBean> showClientByPage(@PathVariable int pageNum,
                                      @PathVariable String clientName,
                                      @PathVariable String clientScope) {
        return iClientService.showClientByPage(pageNum, clientName, clientScope);
    }

    @GetMapping("findClientById/{clientId}")
    private ClientBean findClientById(@PathVariable int clientId) {
        return iClientService.findClientById(clientId);
    }

    @PostMapping("addClient/{clientName}/{clientPeo}/{clientTel}/{clientAddress}/{clientEmail}/{clientScope}")
    private InfoResult addClient(@PathVariable String clientName,
                                 @PathVariable String clientPeo,
                                 @PathVariable String clientTel,
                                 @PathVariable String clientAddress,
                                 @PathVariable String clientEmail,
                                 @PathVariable String clientScope) {
        ClientBean clientBean = new ClientBean();
        clientBean.setClientEmail(clientEmail);
        clientBean.setClientName(clientName);
        clientBean.setClientAddress(clientAddress);
        clientBean.setClientPeo(clientPeo);
        clientBean.setClientTel(clientTel);
        clientBean.setClientScope(clientScope);
        return iClientService.addClient(clientBean);
    }

    @PostMapping("updateClientById/{clientId}/{clientPeo}/{clientTel}/{clientScope}")
    private InfoResult updateClientById(@PathVariable int clientId, @PathVariable String clientPeo, @PathVariable String clientTel, @PathVariable String clientScope) {
        return iClientService.updateClientById(clientId, clientPeo, clientTel, clientScope);
    }

}
